#pragma once
#include <iostream>
#include <stdint.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <string.h>
#include <string>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <arpa/inet.h>
#include <cstdio>



class Client
{
public:
    Client(std::string ip, uint16_t port)
        : _reciveip(ip), _port(port)
    {
    }
    void Init()
    {
        _socketfd = socket(AF_INET, SOCK_DGRAM, 0);
        if (_socketfd < 0)
        {
            perror("socket");
            return;
        }
    }
    void Run()
    {
        std::string s;
        while (true)
        {
            std::cout << "pleas say@";
            std::getline(std::cin, s);
            struct sockaddr_in sockrecive;
            memset(&sockrecive, 0, sizeof(sockrecive));
            sockrecive.sin_family = AF_INET;
            sockrecive.sin_port = htons(_port);
            sockrecive.sin_addr.s_addr = inet_addr(_reciveip.c_str());
            int n = sendto(_socketfd, s.c_str(), s.size(), 0, (const sockaddr *)&sockrecive, sizeof(sockrecive));
            char ch[1024];
           ssize_t ns =  recvfrom(_socketfd,ch,sizeof(ch) - 1,0,nullptr,nullptr);
            ch[ns] = '\0';
            std::cout<<ch<<std::endl;
        }
    }
private:
    std::string _reciveip;
    uint16_t _port;
    int _socketfd;
};